// Installer.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "Installer.h"
#include "ce_setup.h"


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}



codeINSTALL_INIT
Install_Init(
    HWND        hwndParent,
    BOOL        fFirstCall,     // is this the first time this function is being called?
    BOOL        fPreviouslyInstalled,
    LPCTSTR     pszInstallDir
)
{
// TODO: Add custom installation code here

// To continue installation, return codeINSTALL_INIT_CONTINUE
// If you want to cancel installation, 
// return codeINSTALL_EXIT_UNINSTALL
return codeINSTALL_INIT_CONTINUE;

}

void FormatSpecialFolderPath(int nFolder, const LPTSTR path, LPTSTR result)
{	
	TCHAR FolderPath[MAX_PATH];
	SHGetSpecialFolderPath(0, FolderPath, nFolder, 1);
	if (FolderPath[wcslen(FolderPath) - 1] == '\\')
	{
		wsprintf(result, L"%s%s", FolderPath, path); 
	}
	else
	{
		wsprintf(result, L"%s\\%s", FolderPath, path); 
	}
}


const WCHAR* BASE_HTC_MANILA_ADDR = L"Software\\HTC\\Manila";

const LPTSTR ShortcutFileName = L"MTPLoaderStartup.lnk";

codeINSTALL_EXIT
Install_Exit(
    HWND    hwndParent,
    LPCTSTR pszInstallDir,
    WORD    cFailedDirs,
    WORD    cFailedFiles,
    WORD    cFailedRegKeys,
    WORD    cFailedRegVals,
    WORD    cFailedShortcuts
)
{
	TCHAR MtpLaunchPath[MAX_PATH];
	FormatSpecialFolderPath(CSIDL_PROGRAM_FILES, L"ManilaTodayPage\\MTPLoader.exe", MtpLaunchPath);

	TCHAR AutoRunShortcutPath[MAX_PATH];
	FormatSpecialFolderPath(CSIDL_STARTUP, ShortcutFileName, AutoRunShortcutPath);
	TCHAR MtpLaunchPathQuoted[MAX_PATH];
	wsprintf(MtpLaunchPathQuoted, L"\"%s\" /boot", MtpLaunchPath);
	SHCreateShortcut(AutoRunShortcutPath, MtpLaunchPathQuoted);

	PROCESS_INFORMATION pi;

	TCHAR MtpConfiguratorPath[MAX_PATH];
	FormatSpecialFolderPath(CSIDL_PROGRAM_FILES, L"ManilaTodayPage\\MTP Configurator.exe", MtpConfiguratorPath);
	CreateProcess(MtpConfiguratorPath, L"/install", 0, 0, 0, 0, 0, 0, 0, &pi);
	CloseHandle(pi.hProcess);

	HKEY hKey;
	RegOpenKeyEx(HKEY_CURRENT_USER, BASE_HTC_MANILA_ADDR, NULL, KEY_READ, &hKey);
	RegSetValueEx(hKey, L"RSKPath", 0, REG_SZ, (BYTE*)MtpConfiguratorPath, wcslen(MtpConfiguratorPath) * sizeof(TCHAR));
	RegCloseKey(hKey);

	return codeINSTALL_EXIT_DONE;
}

codeUNINSTALL_INIT
Uninstall_Init(
    HWND        hwndParent,
    LPCTSTR     pszInstallDir
)
{
	TCHAR AutoRunShortcutPath[MAX_PATH];
	FormatSpecialFolderPath(CSIDL_STARTUP, ShortcutFileName, AutoRunShortcutPath);
	DeleteFile(AutoRunShortcutPath);

	PROCESS_INFORMATION pi;

	TCHAR MtpConfiguratorPath[MAX_PATH];
	FormatSpecialFolderPath(CSIDL_PROGRAM_FILES, L"ManilaTodayPage\\MTP Configurator.exe", MtpConfiguratorPath);

	CreateProcess(MtpConfiguratorPath, L"/uninstall", 0, 0, 0, 0, 0, 0, 0, &pi);
	CloseHandle(pi.hProcess);

	return codeUNINSTALL_INIT_CONTINUE;
}

codeUNINSTALL_EXIT
Uninstall_Exit(
    HWND    hwndParent
)
{
// TODO: Add custom uninstallation code here

return codeUNINSTALL_EXIT_DONE;
}